 ////////////////////////////////////////////////////////////////////////////////
//
//  THIS CODE IS NOT APPROVED FOR USE IN/ON ANY OTHER UI ELEMENT OR PRODUCT COMPONENT.
//  Copyright (c) 2006 Microsoft Corporation.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

 var L_FIRST_DAY_OF_WEEK = 0;
 var L_SHORTNAME_DOW_text ="周日 周一 周二 周三 周四 周五 周六";
 var L_LONGNAME_DOW_text = "星期日 星期一 星期二 星期三 星期四 星期五 星期六";
 var L_SHORTNAME_MONTH_text = "一 二 三 四 五 六 七 八 九 十 十一 十二";
 var L_LONGNAME_MONTH_text = "一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月";
 
 var L_PREVIOUS_text = "上一个";
 var L_NEXT_text = "下一个";
 var g_filter = 1;
 var g_todayView = true;
 var g_calendar_top_margin_unDocked = 20;
 var g_calendar_top_margin_docked = 20;
 var g_dayView_monthHdr = 5;
 var g_dayView_dayOfWeekHdr = 90;
 var g_calendar_date = new Date().toDateString();
 var g_initDate = new Date().toDateString();
 var g_defaultView = "";
 var g_defaultDOW = "";
 var gView = "month";
 var g_curl_img = null;
 var g_day_view = null;
 var g_DefaultLang = "en";
 var BIDI = "";
 var temp;
 var g_calendarAlarm = null;
 var g_mySettings = new calendarSettings();

////////////////////////////////////////////////////////////////////////////////
//
// Localize
//
////////////////////////////////////////////////////////////////////////////////
BIDI = document.getElementsByTagName("HTML")(0).dir;

if (BIDI == "rtl" || BIDI == "RTL")
{
    temp = L_PREVIOUS_text;
    
    L_PREVIOUS_text = L_NEXT_text;
    L_NEXT_text = temp;
    
    BIDI = "rtl";
}
else
{
    BIDI = "ltr";
}

btnPrevious.alt = L_PREVIOUS_text;
btnNext.alt = L_NEXT_text;

var loc=
{
    load:function()
    {
        this.day=
        {
            first:L_FIRST_DAY_OF_WEEK,
            short:L_SHORTNAME_DOW_text.split(" "),
            long :L_LONGNAME_DOW_text.split(" ")
        };
        this.month=
        {
            short:L_SHORTNAME_MONTH_text.split(" "),
            long :L_LONGNAME_MONTH_text.split(" ")
        };
    }
};

////////////////////////////////////////////////////////////////////////////////
//
// Initialize
//
////////////////////////////////////////////////////////////////////////////////
g_mySettings.loadSettings();

loc.load();

setCalendarAlarm(true);

System.Gadget.visibilityChanged=checkVisibility;
System.Gadget.onDock=Dock;
System.Gadget.onUndock=unDock;
window.onload = System.Gadget.docked?Dock:unDock;

////////////////////////////////////////////////////////////////////////////////
//
// Settings object
//
////////////////////////////////////////////////////////////////////////////////
function calendarSettings()
{
    this.dockedCalendarView       = System.Gadget.Settings.read("dockedCalendarView");
    this.dockedCalendarDivType    = System.Gadget.Settings.read("dockedCalendarDivType");
    this.unDockedCalendarView     = System.Gadget.Settings.read("unDockedCalendarView");
    
////////////////////////////////////////////////////////////////////////////////    
    this.save = function(dockedCalendarView, dockedCalendarDivType)
    {
        if (dockedCalendarView == "")
        {
            dockedCalendarView = "DAY_DOCKED";
            dockedCalendarDivType = "dow";
        }     
        System.Gadget.Settings.write("dockedCalendarView", dockedCalendarView );
        System.Gadget.Settings.write("dockedCalendarDivType", dockedCalendarDivType );
        this.dockedCalendarView  = dockedCalendarView;
        this.dockedCalendarDivType = dockedCalendarDivType;
    }
////////////////////////////////////////////////////////////////////////////////      
    this.saveUnDocked = function(unDockedCalendarView)
    {
        if (unDockedCalendarView == "")
        {
            unDockedCalendarView = "MONTH_UNDOCKED";
        } 
        System.Gadget.Settings.write("unDockedCalendarView", unDockedCalendarView );
        this.unDockedCalendarView  = unDockedCalendarView;   
    }
////////////////////////////////////////////////////////////////////////////////    
    this.loadSettings = function()
    { 
        this.dockedCalendarView     = System.Gadget.Settings.read("dockedCalendarView");
        this.dockedCalendarDivType  = System.Gadget.Settings.read("dockedCalendarDivType"); 
        this.unDockedCalendarView   = System.Gadget.Settings.read("unDockedCalendarView");
      
        if (this.dockedCalendarView == "")
        {
            this.save("DAY_DOCKED","dow");
        }  
        
        if (this.unDockedCalendarView == "")
        {
            this.saveUnDocked("MONTH_UNDOCKED");
        }               
    }
}
////////////////////////////////////////////////////////////////////////////////
//
// determine if gadget is visible
//
////////////////////////////////////////////////////////////////////////////////
function checkVisibility()
{
    var isVisible = System.Gadget.visible;
    var now = new Date().toDateString();
    var initDtd = new Date(g_initDate).toDateString();

    if (! isVisible)
    {
        clearTimeout(g_calendarAlarm);
    }
        
    if (isVisible)
    {
        if (now != initDtd)
        {
            setCalendarAlarm(false);
        }
    }
}
////////////////////////////////////////////////////////////////////////////////
//
// 
//
////////////////////////////////////////////////////////////////////////////////
function setCalendarAlarm(isFirstPass)
{
    var now = new Date();
    var delta = 0;
    var hours = 0;
    var minutes = 0;
    var seconds = 0;
    var MilliSecondsPerDay = (24 * 60 * 60 * 1000);
    
    hours = now.getHours();
    minutes = now.getMinutes();
    seconds = now.getSeconds();
    
    delta = MilliSecondsPerDay - (hours * 60 * 60 * 1000) - (minutes * 60 * 1000) - (seconds * 1000);
       
    g_calendar_date = now.toDateString();
    g_initDate = now.toDateString();
    
    if (! isFirstPass)
    {
        if ( System.Gadget.docked )
        {
            Dock();
        }
        else
        {
            unDock();
        }
    }
    g_calendarAlarm = setTimeout('setCalendarAlarm(false)',delta);  
}
////////////////////////////////////////////////////////////////////////////////
//
// 
//
////////////////////////////////////////////////////////////////////////////////
function swapBackgrounds()
{
    divPrevNext.style.visibility = "visible";
   
    with(document.body.style)
        backgroundImage="url(images/calendar_single.png)";             
}
////////////////////////////////////////////////////////////////////////////////
//
// Docked
//
////////////////////////////////////////////////////////////////////////////////
function Dock(){
    docked.style.display = "none";
    unDocked.style.display = "none"; 
    gView = "month";
    g_curl_img = curlDocked;
    g_day_view = DAY_DOCKED;

    divPrevNext.className = "prevnext";     
    
    if (DAY_UNDOCKED.d != "")
    {
        g_initDate = DAY_UNDOCKED.d;
    }    

    with(document.body.style)
        width=130,
        height=141,
        backgroundRepeat="no-repeat",
        backgroundImage = checkBackground();
 
    Calendar(YEAR_DOCKED).year.initYear(new Date(g_initDate));
    Calendar(MONTH_DOCKED).month.initMonth(new Date(g_initDate));
    Calendar(DAY_DOCKED).day.initDay(new Date(g_initDate));

    MONTH_DOCKED.style.visibility   = "hidden";
    DAY_DOCKED.style.visibility     = "hidden";
    YEAR_DOCKED.style.visibility    = "hidden";
    divPrevNext.style.visibility    = "hidden";
    g_mySettings.loadSettings();
    
    eval( g_mySettings.dockedCalendarView + ".divType = g_mySettings.dockedCalendarDivType");
    swap( eval( g_mySettings.dockedCalendarView ) );
    
    docked.style.display = "";
    g_mySettings.saveUnDocked("");
};
////////////////////////////////////////////////////////////////////////////////
//
//  UnDock
//
////////////////////////////////////////////////////////////////////////////////
function unDock()
{
    docked.style.display = "none";
    g_curl_img = curlUnDocked;
    g_day_view = DAY_UNDOCKED;
    gView = "month";
    tID.divType = "year";
    
    with(document.body.style)
       width=129,
       height=264, 
       backgroundImage = checkBackground(); 
    
    divPrevNext.className = "prevnext_unDocked";
    divPrevNext.style.visibility = "hidden";
    
    Calendar(YEAR_UNDOCKED).year.initYear(new Date(g_initDate));
    Calendar(MONTH_UNDOCKED).month.initMonth(new Date(g_initDate));
    Calendar(DAY_UNDOCKED).day.initDay(new Date(g_initDate));
    
    MONTH_UNDOCKED.style.visibility   = "hidden";
    YEAR_UNDOCKED.style.visibility    = "hidden";
    
    swap( eval( g_mySettings.unDockedCalendarView ) );
    
    unDocked.style.display = "";  
    g_mySettings.save("","");
};
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function setInitDate(o)
{
    g_filter = 0;
    oDate = new Date(g_initDate);
    oDOW  = new Date(o.d);
    
    if (oDate.getMonth() != oDOW.getMonth() | oDate.getFullYear() != oDOW.getFullYear() | gView == "year" )
    {  
        g_initDate = oDOW.toDateString();
        swap(o);
    }
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function checkBackground()
{
    var sRetVal = "";
    var initDate = new Date(g_initDate);
    var isToday = ( initDate.toDateString()== new Date().toDateString() );    

    if (System.Gadget.docked)
    {
            if ( isToday )
            {
                sRetVal = "url(images/calendar_single_orange.png)";
                divBgImageDocked.style.backgroundImage = "url(images/calendar_single_orange.png)";
                dockedCalendarRing.style.visibility = "hidden";   
                g_curl_img.style.visibility     = "hidden";
            }
            else
            {
                sRetVal = "url(images/calendar_single.png)";
                divBgImageDocked.style.backgroundImage = "url(images/calendar_single.png)";
                g_curl_img.style.visibility     = "visible";
                dockedCalendarRing.style.visibility = "visible";     
            }           
    }
    else
    {
            if ( isToday )
            {
                sRetVal = "url(images/calendar_double_orange.png)";
                divBgImageUnDocked.style.backgroundImage = "url(images/calendar_single_bkg_orange.png)";
            }
            else
            {
                sRetVal = "url(images/calendar_double.png)";
                divBgImageUnDocked.style.backgroundImage = "url(images/calendar_single_bkg.png)";
                g_curl_img.style.visibility     = "visible";   
            }           
    }
       
    return sRetVal; 
}
////////////////////////////////////////////////////////////////////////////////
//
// Calendar object 
//
////////////////////////////////////////////////////////////////////////////////
function Calendar(o)
{
////////////////////////////////////////////////////////////////////////////////
//
// Day view
//
////////////////////////////////////////////////////////////////////////////////
    o.day={
        d:new Date(),
        
        html:function(d)
        {
            if (System.Gadget.docked)
            {
                divPrevNext.style.visibility = "hidden";
            }

            this.d = new Date(d);
            
            g_day_view.d = d.toDateString();
            
            var isToday=d.toDateString() == new Date().toDateString();
            
            if (System.Gadget.docked)
            {
                g_initDate = d.toDateString();
            }

            if (isToday)
            {
                g_curl_img.style.visibility = "hidden";
            }
            else
            {
                g_curl_img.style.visibility = "visible";
                g_curl_img.d = ( new Date().toDateString() );
            }
           return "<table id='dowId' valign=center width='100%' cellpadding=0 cellspacing=0><tr><td align=center style='font-size:13px;"+(isToday?"color:white;":"color:black;")+"'>"+loc.month.long[d.getMonth()].toUpperCase()+" "+d.getFullYear()+"</td></tr><tr><td align=center valign=top style='line-height:70px;font-size:75px;"+ (isToday?"color:white;":"color:black;") +"'>"+d.getDate()+"</td></tr><tr><td align=center style='font:14px;"+(isToday?"color:white;":"color:black;")+"'>"+loc.day.long[d.getDay()].toUpperCase()+"</td></tr></table>";
        },

        initDay:function(d)
        {
            o.innerHTML = this.html(d);
        }

    };
    
////////////////////////////////////////////////////////////////////////////////
//
// Month view
//
////////////////////////////////////////////////////////////////////////////////
    o.month=
    {
        html:function(d)
        {
            if(d)this.d=new Date(d);else d=this.d;
            return "<span UNSELECTABLE='on'>"+prevNext(loc.month.short[d.getMonth()]+" "+d.getFullYear().toString().substr(2),monthGrid(d.getYear(),d.getMonth()) )+"</span>";
        },

        initMonth:function(d)
        {
             tID.omo = "";             
             o.innerHTML=this.html(d);
        }
    };
////////////////////////////////////////////////////////////////////////////////
//
// Year view
//
////////////////////////////////////////////////////////////////////////////////
    o.year=
    {
        html:function(d){
            if(d)this.d=new Date(d); else d=this.d;
            var s="<div UNSELECTABLE='on' style='margin:5 0 0 5'>",m=0,y,x;
            for(y=0; y<3; y++, s+="<br>")
                for(x=0; x<4; x++, m++)
                    s+="<var omo tabindex=1 divType='zoomMonth' onkeypress='swap(this);' onclick='swap(this)' m='"+m+"'>"+loc.month.short[m]+"</var>";
            s+="</div>";
            return "<span >"+
                   prevNext(this.d.getFullYear(),s)+
                   "</span>";
        },
        initYear:function(d){
          tID.removeAttribute("omo");
          tID.className = "";
          o.innerHTML = this.html(d);
        }
    };
////////////////////////////////////////////////////////////////////////////////
    function prevNext(tIDle,body)
    {
       tID.innerHTML = tIDle;

       return "<div UNSELECTABLE='on' class='divMonthView_docked'>"+ body + "</div>";
    };
////////////////////////////////////////////////////////////////////////////////
    function monthGrid(year,month){
        
        var dow = new Date().getDay();
        var d=new Date(year,month,1-new Date(year,month,1).getDay()+loc.day.first);
        
        if(d.getMonth()==month && d.getDate()>1)
            d.setDate(d.getDate()-7);
        var s="",today=new Date().toDateString();
        for(var y=0; y<7 && (d.getMonth()<=month || d.getYear()<year); y++){
            for(var x=0; x<7; x++){
                s+="<q divType='dow' class='day"+(x?" lb":"") ;
                if(y){
                    if(d.getMonth()!=month)
                        s+=" dim'";
                    else if(d.toDateString()==today)
                        s+=" today' tabindex=1";
                    else
                        s+="' omo tabindex=1";
                    s+=" d='"+d.toDateString()+"' onkeypress='swap(this);' onclick='swap(this);' >"+d.getDate()+"</q>";
                    d.setDate(d.getDate()+1);
                }else
                    s+=(dow != x ? " name ": ckCurrentMonth(year,month) ) +"'>"+loc.day.short[(x+loc.day.first)%7].substr(0,1)+"</q>";
            }
            s+="<br>";
        }
        return s;
    };
////////////////////////////////////////////////////////////////////////////////    
    function ckCurrentMonth(year,month)
    { 
        var oDate = new Date(g_calendar_date);
        var sRetVal = " name";
        
        if (year == oDate.getFullYear() && month == oDate.getMonth() )
        {
            sRetVal = " dow";
        }
        return sRetVal;
    }
////////////////////////////////////////////////////////////////////////////////
    function dayPos(d){
        var x=(d.getDay()-loc.day.first)%7,
            y=parseInt((d.getDate()+6+loc.day.first-d.getDay())/7);
        if(x<0)x+=7,y--;
        return {x:2+x*17,y:39+y*13}
    };
////////////////////////////////////////////////////////////////////////////////
    function monthPos(d){
        var x=d.getMonth()%4,
            y=parseInt(d.getMonth()/4);
        return {x:7+x*28,y:27+y*28}
    };

    o.tabIndex=1;
    return o;
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function evalPrevNext(direction,o)
{
    var oDate = new Date(g_initDate);

    if (BIDI == "rtl")
    {
        direction *= -1;
    }
      
    
    switch (gView)
    {
        case "month":
                o.divType = "changeMonth";
                oDate.setMonth(oDate.getMonth()+ direction);
                
                break;
        case "year":
                o.divType = "changeYear";
                oDate.setFullYear(oDate.getFullYear()+ direction);
                break;                
    }
   
    g_initDate = oDate.toDateString();
    
    if (event.button == 2)
    {
        if (direction == -1)
        {
            o.src = 'images/bprev.png';
        }
        else
        {
            o.src = 'images/bnext-hot.png';
        }
    }
    swap(o);
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function swap(o)
{
    if (event)
    {
        if (event.button == 2)
        {
            return;
        }
    }
    
    if (System.Gadget.docked)
    {
        renderDocked(o);
    }
    else
    {
        renderUnDocked(o);
    }
    g_filter = 1;
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function renderUnDocked(o)
{
    var sType = o.divType;
    var today = null;
    var isToday = null;
    var oDate = null;

    g_mySettings.saveUnDocked(o.id);
    
    switch (sType)
    {
        case "dow": 
        
                gView = "month";
                if (! System.Gadget.docked)
                {
                    g_filter = 1;
                }
                divFilterBottomUndocked.filters[g_filter].enabled = true;
                divFilterBottomUndocked.filters[g_filter].Apply(); 
                DAY_UNDOCKED.day.initDay(new Date(o.d));
                today=new Date(o.d);
                isToday=today.toDateString()==new Date().toDateString();  
                if (isToday)
                {                                
                    divBgImageUnDocked.style.backgroundImage = "url(images/calendar_single_bkg_orange.png)";
                    g_todayView = true;
                    setTimeout("showBackground(0);",300);
                }
                else
                {
                    divBgImageUnDocked.style.backgroundImage = "url(images/calendar_single_bkg.png)";
                    g_todayView = false;
                    setTimeout("showBackground(1);",300);
                }
                divFilterBottomUndocked.filters[g_filter].Play(); 
                break;
                    
        case "year":
                gView = "year";
                
                divFilterTopUndocked.filters[g_filter].enabled = true;
                divPrevNext.className = "prevnext_unDocked";
                divPrevNext.style.visibility = "visible";                 
                divFilterTopUndocked.filters[g_filter].Apply();
                YEAR_UNDOCKED.year.initYear(new Date(g_initDate));
                YEAR_UNDOCKED.style.visibility = "visible";
                MONTH_UNDOCKED.style.visibility = "hidden";
               
                divFilterTopUndocked.filters[g_filter].Play();
                tID.divType="reset"; 
                break;
                
        case "zoomMonth":
                oDate = new Date(g_initDate);
                oDate.setMonth(o.m);
                g_initDate = oDate.toDateString();
                o.divType = 'day';
                swap(o);
                break;

        case "changeMonth": 
                o.divType = 'day';
                g_filter = 0;
                swap(o);
                break;
                
        case "changeYear": 
                o.divType = 'year';
                g_filter = 0;
                swap(o);
                break;   
                
        case "day":
                gView = "month";
                MONTH_UNDOCKED.month.initMonth(new Date(g_initDate));
                divFilterTopUndocked.filters[g_filter].enabled = true;               
                divFilterTopUndocked.filters[g_filter].Apply();
                YEAR_UNDOCKED.style.visibility = "hidden";
                MONTH_UNDOCKED.style.visibility = "visible";
                divFilterTopUndocked.filters[g_filter].Play();
                showControls(true);
                tID.divType="year";
                break;
    }
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function renderDocked(o)
{ 
    var sType = o.divType;
    var today = null;
    var isToday = null;
    var oDate = null;

   g_mySettings.save(o.id,o.divType);
   
   switch (sType)
    {
        case "changeMonth": 
                o.divType = 'day';
                g_filter = 1;
                swap(o);
                break;
                
        case "changeYear": 
                o.divType = 'year';
                g_filter = 1;
                swap(o);
                break;
                
        case "zoomMonth":
                oDate = new Date(g_initDate);
                oDate.setMonth(o.m);
                g_initDate = oDate.toDateString();
                o.divType = 'day';
                g_filter = 0;
                swap(o);
                break;
                
        case "resetMonth":
                g_initDate = new Date().toDateString();
                o.divType = 'day';
                swap(o);
                break;

        case "dow":
                gView = "dow";
                o.divType = "day";
                divFilterDocked.filters[g_filter].enabled = true;
                divFilterDocked.filters[g_filter].Apply(); 
                Calendar(DAY_DOCKED).day.initDay(new Date(o.d));
                DAY_DOCKED.style.visibility = "visible";
                MONTH_DOCKED.style.visibility = "hidden";   
                today=new Date(o.d);
                isToday=today.toDateString()==new Date().toDateString();  
                if (isToday)
                {                                
                    divBgImageDocked.style.backgroundImage = "url(images/calendar_single_orange.png)";
                    g_todayView = true;
                    setTimeout("showBackground(0);",300);
                }
                else
                {
                    divBgImageDocked.style.backgroundImage = "url(images/calendar_single.png)";
                    g_todayView = false;
                    setTimeout("showBackground(1);",300);
                }
                divFilterDocked.filters[g_filter].Play();
                showControls(false); 
                break;
                
        case "day":
                g_curl_img.style.visibility = "hidden";
                gView = "month";
                MONTH_DOCKED.month.initMonth(new Date(g_initDate));
                divFilterDocked.filters[g_filter].enabled = true;             
                divFilterDocked.filters[g_filter].Apply();
                YEAR_DOCKED.style.visibility = "hidden";
                DAY_DOCKED.style.visibility = "hidden";
                MONTH_DOCKED.style.visibility = "visible";
                divBgImageDocked.style.backgroundImage = "url(images/calendar_single.png)";
                divFilterDocked.filters[g_filter].Play();
                showControls(true);
                showBackground(1);
                tID.divType="year";
                break;

        case "year":
                gView = "year";
                
                g_curl_img.style.visibility = "hidden";
                divFilterDocked.filters[0].enabled = true;
                divFilterDocked.filters[0].Apply();
                YEAR_DOCKED.year.initYear(new Date(g_initDate));
                YEAR_DOCKED.style.visibility = "visible";
                MONTH_DOCKED.style.visibility = "hidden";
                divBgImageDocked.style.backgroundImage = "url(images/calendar_single.png)";
                divFilterDocked.filters[0].Play(); 
                showBackground(1); 
                showControls(true);
                tID.divType="reset";  
                break;                
    }
    

}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function showBackground(skin)
{
    var bgFile =  "url(images/calendar_single.png)";
    
    switch (skin)
    {
        case 0 :
            if (System.Gadget.docked)
            {
                bgFile = "url(images/calendar_single_orange.png)";
            }
            else
            {
                bgFile = "url(images/calendar_double_orange.png)";
            }
            break;
            
        case 1 :
            if (System.Gadget.docked)
            {
                bgFile = "url(images/calendar_single.png)";
            }
            else
            {
                bgFile = "url(images/calendar_double.png)";
            }        
            break;     
    }
    
    document.body.style.backgroundImage = bgFile;
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
function showControls(show)
{
    var showHide = "hidden";
    
    if (show)
    {
        showHide = "visible";
    }
    
    dockedCalendarRing.style.visibility = showHide;
    divPrevNext.style.visibility = showHide;
}
////////////////////////////////////////////////////////////////////////////////
//
//
//
////////////////////////////////////////////////////////////////////////////////
document.onmouseover=document.onmouseout=function(){
    var e=event.srcElement;
    if(e.omo!=null)
        e.className=event.type=="mouseout"?e.className.replace(/ hot/,""):e.className+" hot";
}
